/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

//import java.util.List;
//import javax.management.Query;
//import org.hibernate.Hibernate;
//import org.hibernate.HibernateException;
//import org.hibernate.Session;
import Enum.LoaiNguoiDungEnum;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.LoaiSanPham;
import pojo.NguoiDung;
import pojo.SanPhamCuaHang;
import util.NewHibernateUtil;
//import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class LoaiSanPhamDAO extends BaseDAO{
     public static void ThemLoaiSP(LoaiSanPham loaisp) throws Exception
    {

        Them(loaisp);
    }
     
    public static void XoaLoaiSP(Object[] loaisps) throws Exception
    {
         Xoa(loaisps);
    }
    
     public static void SuaLoaiSP(LoaiSanPham loaisp) throws Exception{
        Sua(loaisp);
    }     
    
      public static List<LoaiSanPham> TimKiemLoaiSanPham(Integer maSanPham,String tenLoaiSanPham,boolean forDialogSearch) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from LoaiSanPham lsp where (lsp.isDelete=false or lsp.isDelete=null)";
//        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode() || forDialogSearch) {
//            string += String.format(" and spch.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
//        }
        if(maSanPham!=null){
            string+=String.format(" and lsp.id=%d", maSanPham);
        }
        if(!tenLoaiSanPham.isEmpty()){
            string+=" and lower(lsp.tenLoaiSanPham) like :tenLoaiSanPham";
        }       
        Query query=session.createQuery(string);
        if(!tenLoaiSanPham.isEmpty()){
            query.setString("tenLoaiSanPham", "%"+tenLoaiSanPham.toLowerCase()+"%");
        }
        List<LoaiSanPham>loaiSanPhams=query.list();
        session.close();
        return loaiSanPhams;
    }
    
}
